package com.mokairui.sort.advanced;

import java.util.Arrays;

/**
 * @Description
 * @Author Mokairui
 * @Since 2021/11/2
 */
public class Selection {
    public static void main(String[] args) {
        int[] array = {8, 10, 11, 40, 22, 27, 2, 8, 18};
        sort(array);
        System.out.println(Arrays.toString(array));
    }

    private static void sort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            // i 代表每轮选择最小元素要交换到的目标索引
            int s = i; // 代表最小元素的索引
            for (int j = s + 1; j < array.length; j++) {
                if (array[s] > array[j]) {
                    s = j;
                }
            }
            if (s != i) {
                swap(array, s, i);
            }
        }
    }

    private static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
